using System;
using UnityEngine;

namespace WorkFlow.Net.Process
{
    [CreateAssetMenu(fileName = "NetPlayerPresData",menuName = "Data/NetPlayerPresData")]
    public class NetPlayerPresData : ScriptableObject
    {
        [SerializeField] private string address;
        [SerializeField] private int port;
        
        [SerializeField] private string totalPath;
        
        [SerializeField] string insertPath;
        [SerializeField] string updatePath;
        [SerializeField] string checkPath;
        [SerializeField] string historyPath;
        [SerializeField] string deletePath;
        [Header("User Detail,不适用 totalPath，请填入完整地址,编译开关中添加：REMOTE_INFO 可加载远程用户数据")]
        [SerializeField] string userDetailPath;
        [Header("treasureChest List,不适用 totalPath，请填入完整地址")]
        [SerializeField] string treasureChestListPath;
        [SerializeField] private GameTagEnum gameTag;
        
#if UNITY_EDITOR
#if !ODIN_INSPECTOR
        [SerializeField] [Sirenix.OdinInspector.BoxGroup("Editor")] private string tokenEditor;
        [SerializeField] [Sirenix.OdinInspector.BoxGroup("Editor")] private string treasureChestIdEditor;
#else
        [Space(10)][Header("编辑器可用")]
        [SerializeField] private string tokenEditor;
        [SerializeField] private string treasureChestIdEditor;
#endif
        public string TreasureChestId => treasureChestIdEditor;
#endif

        public static void LoadData(string dataTail)
        {
            if (_instance != null)
            {
                Debug.Log("Data Already Loaded");
                return;
            }
            string assetName = string.IsNullOrEmpty(dataTail) ? nameof(NetPlayerPresData) : $"{nameof(NetPlayerPresData)}-{dataTail}";
            _instance = Resources.Load<NetPlayerPresData>(assetName);
            _instance._tempAddress = _instance.address;
        }
        
        private static NetPlayerPresData _instance;
        public static NetPlayerPresData Instance => _instance;

// #if UNITY_ANDROID || UNITY_IOS || UNITY_EDITOR
//         public string Token
//         {
//             get
//             {
// #if UNITY_EDITOR && !(UNITY_ANDROID || UNITY_IOS)
//                 return tokenEditor;
// #elif (UNITY_ANDROID || UNITY_IOS) && UNITY_EDITOR
//                 return PlayerPrefs.GetString("token");
// #elif(UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
//                 return TokenCache.Token;
// #endif
//             }
//         }
// #elif UNITY_WEBGL
//             public string Token {
//                 get
//                 {
//                     WebGLLocalStorage.Instance.ReadStorage("token");
//                     string token = WebGLLocalStorage.Instance.GetValue("token");
//                     return token;
//                 }
//             }
// #endif

#if UNITY_EDITOR
        public string Token
        {
            get
            {
                if (EntranceParams.EntranceSceneStarted)
                {
                    return EntranceParams.Token;
                }
                else
                {
                    return tokenEditor;
                }
            }
        }
           
#elif   UNITY_ANDROID || UNITY_IOS
        public string Token => TokenCache.Token;
#elif UNITY_WEBGL
            public string Token {
                get
                {
                    WebGLLocalStorage.Instance.ReadStorage("token");
                    string token = WebGLLocalStorage.Instance.GetValue("token");
                    return token;
                }
            }
#endif

        private string AddressPort
        {
            get
            {
                if (port == 80)
                    return $"{_tempAddress}";
                return $"{_tempAddress}:{port}";
            }
        }

        private string _tempAddress;
        public string Address
        {
            get => _tempAddress;
            set => _tempAddress = value;
        }

        public int Port
        {
            get => port;
            set => port = value;
        }

        public GameTagEnum GameTag
        {
            get => gameTag;
            set => gameTag = value;
        }

        public string InsertUrl => $"{AddressPort}/{totalPath}/{insertPath}";
        
        private string _updateUrl;
        
        public string UpdateUrl =>  $"{AddressPort}/{totalPath}/{updatePath}";

        public string CheckUrl => $"{AddressPort}/{totalPath}/{checkPath}";
        
        private string _deleteUrl;

        public string HistoryUrl => $"{AddressPort}/{totalPath}/{historyPath}/{gameTag.ToString()}";
            
        public string DeleteUrl => $"{AddressPort}/{totalPath}/{deletePath}";
        
        public string UserDetailUrl => $"{AddressPort}/{userDetailPath}";
        
        public string TreasureChestListUrl => $"{AddressPort}/{treasureChestListPath}";
        
        private void OnDestroy()
        {
            _instance = null;
        }
    }
}
